package com.sitech.portal.system.action;

import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import com.sitech.ibnms.core.IPage;
import com.sitech.ibnms.core.JGridBaseAction;
import com.sitech.portal.system.domain.TblSYS_USER_PRIVILEGE;
import com.sitech.portal.system.form.UserPrivilegeForm;
import com.sitech.portal.system.service.ISysLogInmsgService;
import com.sitech.portal.system.service.ISysUserPrivilegeService;

public class UserPrivilegeAction extends JGridBaseAction {
	
	private UserPrivilegeForm theForm;
	private ISysUserPrivilegeService sysUserPrivilegeService;
	private ISysLogInmsgService sysLogInmsgService;
	private List loginMSGList;
	private String unit_name;
	private String flag;

	public UserPrivilegeForm getTheForm() {
		return theForm;
	}

	public void setTheForm(UserPrivilegeForm theForm) {
		this.theForm = theForm;
	}

	public ISysUserPrivilegeService getSysUserPrivilegeService() {
		return sysUserPrivilegeService;
	}

	public void setSysUserPrivilegeService(
			ISysUserPrivilegeService sysUserPrivilegeService) {
		this.sysUserPrivilegeService = sysUserPrivilegeService;
	}

	public ISysLogInmsgService getSysLogInmsgService() {
		return sysLogInmsgService;
	}

	public void setSysLogInmsgService(ISysLogInmsgService sysLogInmsgService) {
		this.sysLogInmsgService = sysLogInmsgService;
	}

	public List getLoginMSGList() {
		return loginMSGList;
	}

	public void setLoginMSGList(List loginMSGList) {
		this.loginMSGList = loginMSGList;
	}

	public String getUnit_name() {
		return unit_name;
	}

	public void setUnit_name(String unit_name) {
		this.unit_name = unit_name;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	/**
	 * 列出所有的特权用户列表
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws BaseException
	 */
	public String loadPrivilegeList() {

		String login_id = "";

		if (theForm.getSEARCH_KEYWORD() == null)
			login_id = "";
		else
			login_id = theForm.getSEARCH_KEYWORD().trim().equals("") ? null
					: theForm.getSEARCH_KEYWORD().trim();
		IPage result = sysUserPrivilegeService.loadUserPrivilegeListByLogin_id(login_id, this.getPageForm());
		this.setResult(result);
		return SUCCESS;
	}

	/**
	 * 编辑用户特权
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws BaseException
	 */
	public String editUserPrivilege() {

		String[] privilege_ids = theForm.getPRIVILEGE_ID();

		IPage result = sysLogInmsgService.findUserCodeLike(null, null, this.getPageForm());
		this.setResult(result);

		if (theForm.getISMP_JSP_COMMAND() != null
				&& theForm.getISMP_JSP_COMMAND().equals("add")) {
			return SUCCESS;
		}
		if (privilege_ids != null && privilege_ids[0] != null) {

			List resultList = sysUserPrivilegeService.getUserPrivilegeListByPrivilege_Id(privilege_ids[0]);
			String login_id = "";
			String unit_id = "";
			String unit_name = "";
			for (int i = 0; i < resultList.size(); i++) {
				HashMap map = (HashMap) resultList.get(i);
				login_id = map.get("LOGIN_ID").toString();
				unit_id += map.get("MANAGE_UNIT_ID").toString() + ",";
				unit_name += map.get("MANAGE_UNIT_NAME").toString() + ",";
			}
			theForm.setENABLE("1");
			theForm.setLOGIN_ID(login_id);
			theForm.setMANAGE_UNIT_ID(unit_id);
		}
		return SUCCESS;
	}

	public String saveUserPrivilege() {

		flag = "true";
		try{
			String login_id = theForm.getLOGIN_ID();
	
			// 先删除数据库中原有改用户的特权记录
			if ("modify".equals(theForm.getISMP_JSP_COMMAND())) {
				sysUserPrivilegeService.deleteUserPrivilegeByLogin_id(login_id);
			}
	
			String enable = theForm.getENABLE();
			String unit_ids = theForm.getMANAGE_UNIT_ID();
			String[] unit_idArray = unit_ids.split(",");
			for (int i = 0; i < unit_idArray.length; i++) {
				if (unit_idArray[i] != null && !"".equals(unit_idArray[i])) {
					String privilege_id = UUID.randomUUID().toString();
					TblSYS_USER_PRIVILEGE privilege = new TblSYS_USER_PRIVILEGE();
					privilege.setENABLE(enable);
					privilege.setLOGIN_ID(login_id);
					privilege.setPRIVILEGE_ID(privilege_id);
					privilege.setMANAGE_UNIT_ID(unit_idArray[i]);
					sysUserPrivilegeService.add(privilege);
				}
			}
		}catch(Exception e){
			flag = e.getMessage();
		}
		return SUCCESS;
	}

	public String deleteUserPrivilegeByPK() {

		flag = "true";
		try{
			String[] privilege_ids = theForm.getPRIVILEGE_ID();
	
			TblSYS_USER_PRIVILEGE privilege = new TblSYS_USER_PRIVILEGE();
			if (privilege_ids != null && privilege_ids[0] != null) {
				privilege.setPRIVILEGE_ID(privilege_ids[0]);
	
				sysUserPrivilegeService.deleteUserPrivilegeByPrivilege_id(privilege);
			}
		}catch(Exception e){
			flag = e.getMessage();
		}
		return SUCCESS;
	}
}
